//
//  SlideView.m
//  SmartContorary
//
//  Created by enghou on 17/6/5.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

#import "SlidePageView.h"

@implementation SlidePageView
{
    UIView *line;
    NSInteger currentIdx;
    NSArray *titleArray;
    CGFloat offX;
    BOOL block;
}

-(void)unlockForRespondsToScroll{
    block = NO;
}

-(void)configTitles:(NSArray *)array{
    titleArray = array;
    offX = 0;
    if ([array count]>0) {
        [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL *stop) {
            [obj removeFromSuperview];
        }];
        CGFloat width = self.frame.size.width / array.count;
        int i = 0;
        for (NSString *title in array) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
            [btn setTitle:title forState:UIControlStateNormal];
            btn.tag = 1000 + i;
            btn.titleLabel.font = [UIFont systemFontOfSize:16];
            if (i == 0) {
                [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            }else{
                [btn setTitleColor:[UIColor colorWithHexString:@"666666"] forState:UIControlStateNormal];
            }
            [btn setFrame:CGRectMake(width * i, 0, width, self.frame.size.height - 2.5)];
            [btn addTarget:self action:@selector(slideToIndex:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
            i++;
        }
        CGFloat wid = self.width / [array count];
        CGFloat originX = width / 2 - wid /2;
        line = [[UIView alloc]initWithFrame:CGRectMake(originX, [self height] - 2.5, wid, 2.5)];
        line.backgroundColor = [UIColor redColor];
        [self addSubview:line];
        currentIdx = 0;
    }
}

-(void)slideToPosition:(NSInteger)index{
    UIButton *btn = (UIButton *)[self viewWithTag:1000 + index];
    if (btn) {
        [self slideToIndex:btn];
    }
}

-(void)slideToOffset:(CGFloat)x{
    if (x != 0) {
        if (!block) {
            CGFloat convertedX = x / 5;
            line.left = offX + convertedX;
        }
    }
}

-(void)slideToIndex:(UIButton *)btn{
    NSInteger index = btn.tag - 1000;
    UIButton *bt = [self viewWithTag:1000+currentIdx];
    [bt setTitleColor:[UIColor colorWithHexString:@"666666"] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    if (currentIdx != index) {
        CGFloat originX = index * line.width;
        offX = originX;
        block = YES;
        [UIView beginAnimations:@"moveAndScale" context:nil];
        [UIView setAnimationDuration:0.25];
        [line setFrame:CGRectMake(originX, [line top], line.width, 2.5)];
        [UIView commitAnimations];
        currentIdx = index;
        if ([self.delegate respondsToSelector:@selector(changeToPage:)]) {
            [self.delegate changeToPage:index];
        }
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
